$(function(){
	/**
	 * 等到Session
	 */
	var url="../../role/getSession.action";
	var data=null;
	$.post(url,data,function(mes){
		if(mes.state==0){
			sweetAlert("抱歉", "你还没有登录", "error");
			setTimeout(function(){
				location.href="../login/login.html";
			},2000);
		}
	},"json");
	
	$("#exit").click(function(){
		swal({ 
		    title: "您确定要退出吗？", 
		    text: "您确定要退出零食进销系统吗？", 
		    type: "info", 
		    showCancelButton: true, 
		    closeOnConfirm: false, 
		    confirmButtonText: "是的，我要退出", 
		    confirmButtonColor: "#ec6c62" 
		}, function() { 
			var url="../../role/exitUser.action";
	    	var data=null;
	    	$.post(url,data,function(mes){
	    		if(mes == 1){
	    			location.href = "../login/login.html";
	    		}
	    	},"json");
		});
	})
	
	//调用findName()方法
	findName();
	
	findByModule();
})

/**
 * 根据Session ID查询模块
 * @returns
 */
function findByModule(){
	
	$.ajaxSetup({
		async:false,
	})
	
	var url="../../role/getUserName.action";
	var data=null;
	var ids=null;
	$.post(url,data,function(mes){
		var url2="../../module/findByModule.action";
		var data2={"userId":mes.userId};
		$.post(url2,data2,function(mes){
			$.each(mes,function(index,item){
					if(item.snMoId == 0){
						$("#LAY-system-side-menu").append("<li id='p"+item.moId+"' data-name='component' class='layui-nav-item'><a href='javascript:;' lay-direction='2'><i class='layui-icon'>"+item.oper+"</i><cite>"+item.moName+"</cite></a>")
						ids=item.moId;
					}else{
						$("#p"+ids).append("<dl class='layui-nav-child'><dd data-name='grid'><a lay-href='"+item.moSite+"'>"+item.moName+"</a></dd></dl></li>")
					}
				});
		},"json");
	},"json");
	
	$.ajaxSetup({
		async:true,
	})
}

/**
 * 获取Session里面的用户名
 * @returns
 */
function findName(){
	var url="../../role/getUserName.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#userName").html(mes.userName);
	},"json");
}


